# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 生产者.py
# @Author: dongguangwen
# @Date  : 2025-04-12 12:43
import pika

# 建立连接
credentials = pika.PlainCredentials('admin', 'password')
connection = pika.BlockingConnection(
    pika.ConnectionParameters('192.168.1.119', credentials=credentials, virtual_host='my_vhost'))
channel = connection.channel()

# ✅ 1. 持久化交换器（direct 类型）
channel.exchange_declare(
    exchange='durable_exchange',
    exchange_type='direct',
    durable=True   # 🔥 持久化交换器
)

# ✅ 2. 持久化队列
channel.queue_declare(
    queue='durable_queue',
    durable=True   # 🔥 持久化队列
)

# ✅ 3. 将队列绑定到交换器上
channel.queue_bind(
    queue='durable_queue',
    exchange='durable_exchange',
    routing_key='durable_key'
)

# ✅ 4. 发送一条持久化消息
channel.basic_publish(
    exchange='durable_exchange',
    routing_key='durable_key',
    body='这是一条持久化的消息',
    properties=pika.BasicProperties(
        delivery_mode=2  # 🔥 设置消息为持久化
    )
)

print("[✔] 持久化消息已发送！")

connection.close()
